package ph.com.gs3.formalistics.presenter.fragments.views;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.view.customviews.SignatureView;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class SignatureEditViewFragment extends Fragment {

	public static final String TAG = SignatureEditViewFragment.class.getSimpleName();

	private SignatureView svSignature;

	private Button bSave;
	private Button bCancel;

	private SignatureEditViewFragmentActionListener actionListener;

	@Override
	public void onAttach(Activity activity) {

		try {
			actionListener = (SignatureEditViewFragmentActionListener) activity;
		} catch (ClassCastException e) {
			throw new RuntimeException("The class " + activity.getClass().getSimpleName()
			        + " must implement SignatureEditViewFragmentActionListener");
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_signature_edit, container, false);

		svSignature = (SignatureView) rootView.findViewById(R.id.Signature_svSignature);

		bSave = (Button) rootView.findViewById(R.id.Signature_bSave);
		bCancel = (Button) rootView.findViewById(R.id.Signature_bCancel);

		bSave.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				actionListener.onSaveSignature(svSignature.getImage());
			}
		});

		bCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				actionListener.onCancel();
			}
		});

		return rootView;
	}

	public static interface SignatureEditViewFragmentActionListener {

		public void onSaveSignature(Bitmap signBitmap);

		public void onCancel();

	}

}
